#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int QDataType;
typedef struct QueueNode {
	QDataType val;
	struct QueueNode* next;
}QNode;

typedef struct Queue {
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

void QueuePush(Queue* pq, QDataType x) {
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL) {
		perror("malloc fail");
		return;
	}
	newnode->val = x;
	newnode->next = NULL;
	if (pq->ptail == NULL) {
		pq->phead = pq->ptail = newnode;
	}
	else {
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}

void QueuePop(Queue* pq) {
	assert(pq);
	assert(pq->phead);
	QNode* del = pq->phead;
	pq->phead = pq->phead->next;
	free(del);
	del = NULL;
	if (pq->phead == NULL) {
		pq->ptail = NULL;
	}
	pq->size--;
}

QDataType QueueFront(Queue* pq) {
	assert(pq);
	assert(pq->phead);
	return pq->phead->val;
}

QDataType QueueBack(Queue* pq) {
	assert(pq);
	assert(pq->ptail);
	return pq->ptail->val;
}

int main() {
	struct Queue q = { NULL, NULL, 0 };
	int n, x, cod;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &cod);
		if (cod == 1) {
			scanf("%d", &x);
			QueuePush(&q, x);
		}
		if (cod == 2) {
			QueuePop(&q);
		}
		if (cod == 3) {
			printf("%d %d\n", QueueFront(&q), QueueBack(&q));
		}
	}
	return 0;
}
